package game.server;

import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/websocket")
public class WebSocketServer {

	@OnMessage
	public void onMessage(String message, Session session) throws IOException, InterruptedException {
		System.out.println("服务端接收消息：" + message);
		
		session.getBasicRemote().sendText("服务端开始返回消息");
		
		for(int i = 0;i < 10;i ++) {
			Thread.sleep(3000);
			session.getBasicRemote().sendText("这是第" + (i + 1) + "条消息");
		}
		
		session.getBasicRemote().sendText("服务端消息结束");
		session.close();
	}
	
	@OnOpen
	public void onOpen() {
		System.out.println("与客户端创建连接");
	}
	
	@OnClose
	public void onClose() {
		System.out.println("连接已关闭");
	}
	
}
